package com.marktotop.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.marktotop.entity.system.User;
import com.marktotop.error.SystemException;
import com.marktotop.response.ResultBean;
import com.marktotop.service.IDepartmentService;
import com.marktotop.service.IUserService;
import com.marktotop.vo.system.DeanVO;
import com.marktotop.vo.system.DepartmentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Api(tags = "系统模块-部门相关接口")
@RestController
@RequestMapping("/system/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IUserService userService;


    /**
     * 部门列表
     *
     * @return
     */
    @ApiOperation(value = "部门列表", notes = "部门列表,根据部门名模糊查询")
    @RequestMapping(value = "/findList",method = RequestMethod.GET)
    public ResultBean findDepartmentList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize") Integer pageSize,
                                         DepartmentVO departmentVO) {
        return departmentService.findDepartmentList(pageNum, pageSize, departmentVO);
    }


    /**
     * 所有部门
     *
     * @return
     */
    @ApiOperation(value = "所有部门",notes = "展示所有部门列表")
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public ResultBean findAll() {
        List<DepartmentVO> departmentVOS = departmentService.findAllVO();
        return ResultBean.ok().data("result",departmentVOS);
    }


    /**
     * 查找部门主任
     *
     * @return
     */
    @ApiOperation(value = "部门主任", notes = "查找部门主任,排除掉已经禁用的用户")
    @GetMapping("/findDeanList")
    public ResultBean findDeanList() {
        List<DeanVO> managerList = departmentService.findDeanList();
        return ResultBean.ok().data("result",managerList);
    }


    /**
     * 添加部门
     *
     * @return
     */
    @ApiOperation(value = "添加部门",notes = "添加新的部门信息")
    @RequiresPermissions({"department:add"})
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultBean add(@RequestBody @Validated DepartmentVO departmentVO) {
        departmentService.addDept(departmentVO);
        return ResultBean.ok().message("添加新部门操作成功!");
    }


    /**
     * 编辑部门
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "编辑部门",notes = "编辑部门信息")
    @RequiresPermissions({"department:edit"})
    @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
    public ResultBean edit(@PathVariable Long id) throws SystemException {
        DepartmentVO departmentVO = departmentService.editDept(id);
        return ResultBean.ok().data("result",departmentVO);
    }


    /**
     * 更新部门
     *
     * @return
     */
    @ApiOperation(value = "更新部门",notes = "更新部门信息")
    @RequiresPermissions({"department:update"})
    @RequestMapping(value = "/update/{id}",method = RequestMethod.PUT)
    public ResultBean update(@PathVariable Long id, @RequestBody @Validated DepartmentVO departmentVO) throws SystemException {
        departmentService.updateDept(id, departmentVO);
        return ResultBean.ok().message("更新部门信息操作成功!");
    }


    /**
     * 删除部门
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除部门",notes = "删除某条部门信息")
    @RequiresPermissions({"department:delete"})
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResultBean delete(@PathVariable Long id) {
        // 判断部门内是否存在用户
        if (userService.findUserByDept(id)){
            if (!departmentService.removeById(id)){
                return ResultBean.error().message("删除部门信息失败!");
            }
            return ResultBean.ok().message("删除部门信息成功!");
        }
        return ResultBean.error().message("删除部门内有员工存在，调整后删除！");
    }

}
